{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> start QUANTAXIS\n",
      "QUANTAXIS>> Welcome to QUANTAXIS, the Version is 1.1.0\n",
      "QUANTAXIS>>  \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``########`````##````````##``````````##`````````####````````##```##########````````#``````##``````###```##`````######`` \n",
      "  `##``````## ```##````````##`````````####````````##`##```````##```````##```````````###``````##````##`````##```##`````##` \n",
      "  ##````````##```##````````##````````##`##````````##``##``````##```````##``````````####```````#```##``````##```##``````## \n",
      "  ##````````##```##````````##```````##```##```````##```##`````##```````##`````````##`##```````##`##```````##````##``````` \n",
      "  ##````````##```##````````##``````##`````##``````##````##````##```````##````````##``###```````###````````##`````##`````` \n",
      "  ##````````##```##````````##``````##``````##`````##`````##```##```````##```````##````##```````###````````##``````###```` \n",
      "  ##````````##```##````````##`````##````````##````##``````##``##```````##``````##``````##`````##`##```````##````````##``` \n",
      "  ##````````##```##````````##````#############````##```````##`##```````##`````###########`````##``##``````##`````````##`` \n",
      "  ###```````##```##````````##```##```````````##```##```````##`##```````##````##`````````##```##```##``````##```##`````##` \n",
      "  `##``````###````##``````###``##`````````````##``##````````####```````##```##``````````##``###````##`````##````##`````## \n",
      "  ``#########``````########```##``````````````###`##``````````##```````##``##````````````##`##``````##````##`````###``### \n",
      "  ````````#####`````````````````````````````````````````````````````````````````````````````````````````````````````##``  \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``````````````````````````Copyright``yutiansut``2018``````QUANTITATIVE FINANCIAL FRAMEWORK````````````````````````````` \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " \n"
     ]
    }
   ],
   "source": [
    "import QUANTAXIS as QA\n",
    "\n",
    "import threading\n",
    "import pandas as pd\n",
    "\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sub\n",
      "subscribe\n",
      "[<_MainThread(MainThread, started 21604)>, <Thread(Thread-4, started daemon 19260)>, <Heartbeat(Thread-5, started daemon 12700)>, <HistorySavingThread(IPythonHistorySavingThread, started 20440)>, <ParentPollerWindows(Thread-3, started daemon 23924)>, <Thread(pymongo_server_monitor_thread, started daemon 23488)>, <Thread(pymongo_kill_cursors_thread, started daemon 4404)>, <Thread(pymongo_kill_cursors_thread, started daemon 23068)>,  <QA_ENGINE with ['ORDER', 'backtest'] kernels ident 18360>, <QA_ThreadORDER  id=2436982472152 ident 11468>, <QA_Threadbacktest  id=2437091804832 ident 2224>]\n"
     ]
    }
   ],
   "source": [
    "# utf-8\n",
    "import QUANTAXIS as QA\n",
    "\n",
    "market = QA.QA_Market(if_start_orderthreading=True)\n",
    "portfolio= QA.QA_Portfolio()\n",
    "# 创建两个account\n",
    "# 这里是创建一个资产组合,然后在组合里面创建两个account  你可以想象成股票里面的两个策略账户\n",
    "# 然后返回的是这个账户的id\n",
    "a_1 = portfolio.new_account()\n",
    "a_1.reset_assets(100000000)\n",
    "a_1.frequence = QA.FREQUENCE.ONE_MIN\n",
    "market.start()\n",
    "market._sync_orders()\n",
    "market.connect(QA.BROKER_TYPE.BACKETEST)\n",
    "market.login(QA.BROKER_TYPE.BACKETEST, a_1.account_cookie, a_1)\n",
    "\n",
    "market.order_handler.monitor\n",
    "print(threading.enumerate())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> MARKET WARING: SOMEING WRONG WITH ORDER \n",
      " \n",
      "QUANTAXIS>> code 000001 date 2018-08-14 14:58:00 price 0 order_model MARKET amount_model by_amount\n",
      "QUANTAXIS>> MARKET WARING: SOMEING WRONG WITH ORDER \n",
      " \n",
      "QUANTAXIS>> code 000004 date 2018-08-14 14:58:00 price 0 order_model MARKET amount_model by_amount\n"
     ]
    }
   ],
   "source": [
    "for code in ['000001', '000002', '000004', '600010', '000007','600000']:\n",
    "    market.insert_order(a_1.account_cookie,code=code,\n",
    "                            price=0,\n",
    "                            amount=1000,\n",
    "                            time='2018-08-14 14:58:00',\n",
    "                            towards=QA.ORDER_DIRECTION.BUY,\n",
    "                            order_model=QA.ORDER_MODEL.MARKET,\n",
    "                            amount_model=QA.AMOUNT_MODEL.BY_AMOUNT,\n",
    "                            market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                            frequence=QA.FREQUENCE.ONE_MIN,\n",
    "                            broker_name=QA.BROKER_TYPE.BACKETEST,\n",
    "    )\n",
    "while True:\n",
    "    if len(market.order_handler.order_queue.untrade)==0:\n",
    "        break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "market.settle_order()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Acc_HgDIe1qk', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '000002', None, 1, 22.33, 22.33, 'trade_success', 1000, 1000, 22335.5825, 0, 'Order_LYjbzuBt', 'Trade_92iCzWUE']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_LYjbzuBt datetime:2018-08-14 14:58:00 code:000002 amount:1000 price:22.33 towards:1 btype:stock_cn order_id:Order_LYjbzuBt account:Acc_HgDIe1qk status:queued >\n",
      "queued\n",
      "['Acc_HgDIe1qk', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '600010', None, 1, 1.63, 1.63, 'trade_success', 1000, 1000, 1635.0, 0, 'Order_FLyq7JBN', 'Trade_USTZ08F1']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_FLyq7JBN datetime:2018-08-14 14:58:00 code:600010 amount:1000 price:1.63 towards:1 btype:stock_cn order_id:Order_FLyq7JBN account:Acc_HgDIe1qk status:queued >\n",
      "queued\n",
      "['Acc_HgDIe1qk', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '000007', None, 1, 0.0, 13.23, 'trade_price_limit', 1000, 0, 5, 0, 'Order_OIuxT70K', 'Trade_jn5GlHwK']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_OIuxT70K datetime:2018-08-14 14:58:00 code:000007 amount:1000 price:13.23 towards:1 btype:stock_cn order_id:Order_OIuxT70K account:Acc_HgDIe1qk status:queued >\n",
      "queued\n",
      "['Acc_HgDIe1qk', '2018-08-14 14:58:00', '2018-08-14 14:58:00', '600000', None, 1, 10.07, 10.065000000000001, 'trade_success', 1000, 1000, 10070.000000000002, 0, 'Order_JkyYMKu3', 'Trade_JNOqDLPG']\n",
      "on_insert_order\n",
      "< QA_Order realorder_id Order_JkyYMKu3 datetime:2018-08-14 14:58:00 code:600000 amount:1000 price:10.065000000000001 towards:1 btype:stock_cn order_id:Order_JkyYMKu3 account:Acc_HgDIe1qk status:queued >\n",
      "queued\n",
      "SETTLE ORDERHANDLER\n",
      "11468\n",
      "receive deal\n",
      "11468\n",
      "receive deal\n",
      "11468\n",
      "11468\n",
      "receive deal\n",
      "True\n",
      "[]\n",
      "settled\n"
     ]
    }
   ],
   "source": [
    "market.trade_engine.join()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>code</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>cash</th>\n",
       "      <th>order_id</th>\n",
       "      <th>realorder_id</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>commission</th>\n",
       "      <th>tax</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>000002</td>\n",
       "      <td>22.33</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997763e+07</td>\n",
       "      <td>Order_LYjbzuBt</td>\n",
       "      <td>Order_LYjbzuBt</td>\n",
       "      <td>Trade_lQ7NMwHu</td>\n",
       "      <td>Acc_HgDIe1qk</td>\n",
       "      <td>5.5825</td>\n",
       "      <td>33.495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600010</td>\n",
       "      <td>1.63</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.997600e+07</td>\n",
       "      <td>Order_FLyq7JBN</td>\n",
       "      <td>Order_FLyq7JBN</td>\n",
       "      <td>Trade_7MW4lamQ</td>\n",
       "      <td>Acc_HgDIe1qk</td>\n",
       "      <td>0.4075</td>\n",
       "      <td>2.445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-08-14 14:58:00</td>\n",
       "      <td>600000</td>\n",
       "      <td>10.07</td>\n",
       "      <td>1000</td>\n",
       "      <td>9.996591e+07</td>\n",
       "      <td>Order_JkyYMKu3</td>\n",
       "      <td>Order_JkyYMKu3</td>\n",
       "      <td>Trade_ZzMWFyil</td>\n",
       "      <td>Acc_HgDIe1qk</td>\n",
       "      <td>2.5175</td>\n",
       "      <td>15.105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime    code  price  amount          cash        order_id  \\\n",
       "0  2018-08-14 14:58:00  000002  22.33    1000  9.997763e+07  Order_LYjbzuBt   \n",
       "1  2018-08-14 14:58:00  600010   1.63    1000  9.997600e+07  Order_FLyq7JBN   \n",
       "2  2018-08-14 14:58:00  600000  10.07    1000  9.996591e+07  Order_JkyYMKu3   \n",
       "\n",
       "     realorder_id        trade_id account_cookie  commission     tax  \n",
       "0  Order_LYjbzuBt  Trade_lQ7NMwHu   Acc_HgDIe1qk      5.5825  33.495  \n",
       "1  Order_FLyq7JBN  Trade_7MW4lamQ   Acc_HgDIe1qk      0.4075   2.445  \n",
       "2  Order_JkyYMKu3  Trade_ZzMWFyil   Acc_HgDIe1qk      2.5175  15.105  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1.history_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.if_start_orderquery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
